// pages/index/dayi/submitQuestion/submitQuestion.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {  
      url:'',
    topic: '',  
    content: ''  
  },  
  
  onTopicInput: function(e) {  
    this.setData({  
      topic: e.detail.value  
    });  
  },  
  
  onContentInput: function(e) {  
    this.setData({  
      content: e.detail.value  
    });  
  },  
  
  onSendTap: function() {  
    const { topic, content } = this.data;  
    if (!topic) {  
      wx.showToast({  
        title: '标题不能为空',  
        icon: 'none',  
        duration: 2000  
      });  
      return;  
    }  
    let that = this
    // console.log(app.globalData.userInfo)
    wx.request({
      url:that.data.url+'question',
      data:{
        questionTitle:that.data.topic,
        questionContent:that.data.content,
        userId:app.globalData.userInfo.userId,
      },
      method:'post',
      success(res){
          if(res.data.status == 200){
            wx.showToast({
                title: '发送成功',
              });

              wx.request({
                url: that.data.url+'user',
                method:'PUT',
                data:{
                    userId:app.globalData.userInfo.userId,
                    userPoint:app.globalData.userInfo.userPoint+2
                }
              })
              app.globalData.userInfo.userPoint = app.globalData.userInfo.userPoint+2
              that.setData({
                topic: '',  
                content: ''  
              })
          }else{
            wx.showToast({
                title: '发送失败',
              })
          }
      }
    })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
        url:app.globalData.URL
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})